// pages/search/search.js
var common = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      type:1,
      searchSta: true,
      perlist:[],
      searchT: '',
      typeh:2,
      sta:true,
      pageindex: 1,
      pagesize:10,
      atrIDs: '',
      refresh: true,
      storelist:[],
      pdtIDs: '',
      infoList: [],
      cur_index: 1,
      pageindexs: 1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.personSearch()
  },
    searchtitle:function (e) {
        var that = this;
        that.setData({
            sta: false,
            searchT:e.currentTarget.dataset.title
        })
        that.storeList()
    },
  search:function () {
      var that = this;
      // if (that.data.searchT == '') {
      //   common.info('请输入搜索关键字')
      //     return false
      // }
      that.setData({
          sta: false,
          storelist:[],
          infoList: [],
          pageindex: 1,
          searchSta: true,
          pageindexs: 1
      })
      if (that.data.searchT != '') {
          common.ajax({
              url: 'PersonMain/AddKeyword',
              data: {
                  KeywordName: that.data.searchT
              },
              loading: '加载中...',
              userinfo: false,
              success: function (res) {

              }
          })
      }
      that.storeList()
      that.infoList()
  },
    business:function (e) {
    var that = this
       if (e.currentTarget.dataset.index == 1) {
         that.setData({
             storelist: [],
             refresh: true,
             pageindex: 1,
             cur_index: e.currentTarget.dataset.index
         })
         console.log(that.data.pageindex)
         that.storeList()
       } else {
         that.setData({
             infoList: [],
             refresh: true,
             pageindexs: 1,
             cur_index: e.currentTarget.dataset.index
         })
           console.log(that.data.pageindexs)
           that.infoList();
       }
    },
    infoList:function () {
      var that = this;
      common.ajax({
          url: 'PersonMain/GetMicroProductInfoList',
          data: {
              pageIndex: that.data.pageindexs,
              pageSize: that.data.pagesize,
              searchStr: that.data.searchT,
              pdtIDs: that.data.pdtIDs
          },
          // loading: '加载中...',
          userinfo: false,
          success: function (res) {
             if (res.data == null) {
                 that.setData({
                     refresh: false
                 })
               return false
             }
              if (res.data.length < that.data.pagesize) {
                  that.setData({
                      refresh: false
                  })
              } else {
                  that.data.pageindexs ++
                  that.setData({
                      pageindexs: that.data.pageindexs
                  })
              }
              if (res.data.length > 0) {
                  that.data.infoList = that.data.infoList.concat(res.data)
                  that.setData({
                      infoList: that.data.infoList
                  })
                  console.log( that.data.infoList)
              }
          }
      })
  },
  storeList:function () {
      var that = this;
      console.log(that.data.pageindex)
      console.log(1233)
      common.ajax({
          url: 'PersonMain/GetMicroActorInfoList',
          data: {
              pageIndex: that.data.pageindex,
              pageSize: that.data.pagesize,
              searchStr: that.data.searchT,
              atrIDs: that.data.atrIDs
          },
          // loading: '加载中...',
          userinfo: false,
          success: function (res) {
              if (res.data == null) {
                  that.setData({
                      refresh: false
                  })
                  return false
              }
              if (res.data.length < that.data.pagesize) {
                  that.setData({
                      refresh: false
                  })
              } else {
                  that.data.pageindex ++
                  that.setData({
                      pageindex: that.data.pageindex
                  })
              }
              if (res.data.length > 0) {
                  that.data.storelist = that.data.storelist.concat(res.data)
                  that.setData({
                      storelist: that.data.storelist
                  })
              }
          }
      })
  },
  searchTitl:function (event) {
      var that = this
      if (event.detail.value == '') {
          that.setData({
              storelist:[],
              infoList: [],
              pageindex: 1,
              searchSta: true
          })
          that.setData({
              sta: false,
              searchT:''
          })
          that.storeList()
          that.infoList()
      }
     this.setData({
         searchT:event.detail.value
     })
  },
  catch:function () {
      var that = this;
      that.setData({
          perli: []
      })
      common.ajax({
          url: 'PersonMain/GetKeywords',
          data: {
              type: that.data.typeh
          },
          loading: '加载中...',
          userinfo: false,
          success: function (res) {
            that.data.typeh ++
              that.setData({
                  perli: res.data,
                  typeh: that.data.typeh
              })
          }
      })
  },
  personSearch: function () {
        var that = this;
        common.ajax({
            url: 'PersonMain/GetKeywords',
            data: {
                type: that.data.type
            },
            loading: '加载中...',
            userinfo: false,
            success: function (res) {
               if (res.data.length == 0) {
                  that.setData({
                      searchSta: false
                  })
                   return false
               }
               that.setData({
                   perlist: res.data,
                   perli: res.data
               })
            }
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
    infodetail:function (e) {
        console.log(e)
        wx.navigateTo({
            url:'../goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    },
    storedetail:function (e) {
      console.log(e)
        wx.navigateTo({
            url:'../companyDetail/companyDetail?atrID=' + e.currentTarget.dataset.id
        })
    },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      var that = this;
      that.setData({
          refresh: true,
          pagesize: 10,
          pageindex:1,
          pageindexs:1
      });
      if (that.data.cur_index == 1) {
          that.setData({
              storelist: []
          });
          that.storeList()
          wx.stopPullDownRefresh()
      } else {
          that.setData({
              infoList: []
          });
          that.infoList()
          wx.stopPullDownRefresh()
      }

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var that = this;
      if (that.data.refresh) {
          if (that.data.cur_index == 1) {
              that.storeList()
          } else {
              that.infoList()
          }
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})